package pl.plesken.html;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import pl.plesken.xml.EditXMLFile;

public class HTMLCreator {

	private Map<String, Transformer> transformerCache = new HashMap<String, Transformer>();
    private TransformerFactory transformerFactory;
    
    
    public void movieHTML(String movie) {
        try {
        	EditXMLFile xml = new EditXMLFile();
        	
            String skinHome = xml.skinDir(xml.loadData(movie + ".xml"));
            File htmlFile = new File(movie+ ".html");
            File xmlFile = new File(movie + ".xml");
            Source xmlSource;

            if (!(movie + ".xml").isEmpty()) {

                Transformer transformer = getTransformer(skinHome + File.separator + "detail.xsl");
                xmlSource = new StreamSource(new FileInputStream(xmlFile));
                FileOutputStream outStream = new FileOutputStream(htmlFile);
                Result xmlResult = new StreamResult(outStream);

                transformer.transform(xmlSource, xmlResult);
                outStream.flush();
                outStream.close();
            }
        } catch (Exception e) {
            System.err.println("Failed generating HTML from xml file movie: " + movie + ".xml");
            e.printStackTrace();
        }
    }
	
    private Transformer getTransformer(String xslFileName) throws TransformerConfigurationException {
        if (! transformerCache.containsKey(xslFileName)) {
            if (transformerFactory == null) {
                transformerFactory = TransformerFactory.newInstance();
            }
            Source xslSource = new StreamSource(new File(xslFileName));
            Transformer transformer = transformerFactory.newTransformer(xslSource);
            transformerCache.put(xslFileName, transformer);
        }
        return transformerCache.get(xslFileName);
    }

    public void createPlaylistCollection(String dir, String baseName) throws FileNotFoundException, UnsupportedEncodingException{
    	EditXMLFile xml = new EditXMLFile();
    	//StringBuffer playlistFile = new StringBuffer();
    	
    	String file = dir + File.separator + baseName + ".xml";
    	
    	xml.files(file);
    	
    	if (xml.getRowFiles().size() > 1) {
        	File playlist = new File(dir + File.separator + baseName + ".playlist.jsp");
        	PrintWriter writer = new PrintWriter(playlist, "UTF-8");
        	
    		for (int i = 0; i < xml.getRowFiles().size(); i++) {
    			String[] tab = xml.getRowFiles().get(i).split("<BREAK_TABLE>");
    			//playlistFile.append(tab[1] + " " + tab[0] + "|0|0|" + );
    			writer.println(tab[1] + " " + tab[0] + "|0|0|" + tab[2] + "|");
    		}
    		
    	    writer.flush();
    	    writer.close();
    	}

    }

    
}
